function form_ckeditor(id, editor){
	var base_url = $("#base_url_global").val();
	$(document).ready(function(){
		var editor_language_interface = $("#ckeditor_language_interface").val();
		editor = CKEDITOR.replace(id,{
			language : editor_language_interface,
	       	filebrowserBrowseUrl      : base_url + "static/assets/js/ckfinder/ckfinder.html",
			filebrowserImageBrowseUrl : base_url + "static/assets/js/ckfinder/ckfinder.html?type=Images",
			filebrowserFlashBrowseUrl : base_url + "static/assets/js/ckfinder/ckfinder.html?type=Flash",
			filebrowserUploadUrl      : base_url + "static/assets/js/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files",
			filebrowserImageUploadUrl : base_url + "static/assets/js/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images",
			filebrowserFlashUploadUrl : base_url + "static/assets/js/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash",
	    });
	});
	return editor;		
}
function BrowseServer( imageBox, hiddenImage){
	var base_url = $("#base_url_global").val();
	var finder = new CKFinder() ;
	finder.BasePath = base_url + "static/assets/js/ckfinder/";
	finder.SelectFunction = SetFileField ;
	finder.SelectFunctionData = imageBox + "-" + hiddenImage;
	finder.Popup() ;
}
function BrowseImageServer(){
	var base_url = $("#base_url_global").val();
	var finder = new CKFinder() ;
	finder.BasePath = base_url + "static/assets/js/ckfinder/";
	finder.Popup() ;
}
function SetFileField( fileUrl, data ){
	var inputs = data["selectActionData"];
	var arraySplited = inputs.split("-");
	var imageBox = arraySplited[0];
	var hiddenImage = arraySplited[1];
	document.getElementById(imageBox).src = fileUrl ;
	document.getElementById(hiddenImage).value = fileUrl ;
}

function BrowseSoundServer( imageBox, hiddenImage){
	var base_url = $("#base_url_global").val();
	
	var finder = new CKFinder() ;
	finder.BasePath = base_url + "static/assets/js/ckfinder/";
	finder.SelectFunction = SetSoundFileField ;
	finder.SelectFunctionData = imageBox + "-" + hiddenImage;
	finder.Popup() ;
}
function SetSoundFileField( fileUrl, data ){
	var inputs = data["selectActionData"];
	var arraySplited = inputs.split("-");
	var imageBox = arraySplited[0];
	var hiddenImage = arraySplited[1];
	var base_url = $("#base_url_global").val();
	
	document.getElementById(imageBox).src = base_url + 'static/assets/images/sound_file.png';
	document.getElementById(hiddenImage).value = fileUrl ;
}

function show_info_message(msg){
	jQuery().showRibbonMessage({ message : msg, color:'green' });
	setTimeout(function(){
		jQuery().disposeRibbonMessage();
	}, 1500);
}
function confirmDelete(url, confirm_message) {
	if (url) {
		var ret = confirm(confirm_message);
		if (ret) {
			location.href = url;
		}
	}
}
function confirmStopSend(url, confirm_message) {
	if (url) {
		var ret = confirm(confirm_message);
		if (ret) {
			location.href = url;
		}
	}
}
function datepicker_util(){
	var base_url = $("#base_url_global").val();
	try{
		$( ".datepicker" ).datepicker({
			showOn: "button",
			buttonImage: base_url+"static/assets/images/calendar.gif",
			buttonImageOnly: true,
			dateFormat: "yy-mm-dd",
			timeFormat: "HH:mm:00"
		});	
	}catch(ex){}
}
function datetimepicker_util(){
	var base_url = $("#base_url_global").val();
	try{
		$( ".datetimepicker" ).datetimepicker({
			showOn: "button",
			buttonImage: base_url+"static/assets/images/calendar.gif",
			buttonImageOnly: true,
			dateFormat: "yy-mm-dd",
			timeFormat: "HH:mm:00"
		});	
	}catch(ex){}
}

function submit_my_form(){
	if(!$("#my_form").attr('submited')){
		$("#my_form").attr('submited', 'true');
		$("#my_form").submit();
		return true;
	}
	return false;
}
function string_to_slug(str) {
  str = str.replace(/^\s+|\s+$/g, ''); // trim
  str = str.toLowerCase();
  
  // remove accents, swap Ã± for n, etc
  var from = "Ã Ã¡áº¡áº£Ã£Ã¢áº§áº¥áº­áº©áº«Äƒáº±áº¯áº·áº³áºµÃ¨Ã©áº¹áº»áº½Ãªá»áº¿á»‡á»ƒá»…Ã¬Ã­á»‹á»‰Ä©Ã²Ã³á»á»ÃµÃ´á»“á»‘á»™á»•á»—Æ¡á»á»›á»£á»Ÿá»¡Ã¹Ãºá»¥á»§Å©Æ°á»«á»©á»±á»­á»¯á»³Ã½á»µá»·á»¹Ä‘Ã€Ãáº áº¢ÃƒÃ‚áº¦áº¤áº¬áº¨áºªÄ‚áº°áº®áº¶áº²áº´ÃˆÃ‰áº¸áººáº¼ÃŠá»€áº¾á»†á»‚á»„ÃŒÃá»Šá»ˆÄ¨Ã’Ã“á»Œá»ŽÃ•Ã”á»’á»á»˜á»”á»–Æ á»œá»šá»¢á»žá» Ã™Ãšá»¤á»¦Å¨Æ¯á»ªá»¨á»°á»¬á»®á»²Ãá»´á»¶á»¸ÄÂ·/_,:;";
  var to   = "aaaaaaaaaaaaaaaaaeeeeeeeeeeeiiiiiooooooooooooooooouuuuuuuuuuuyyyyydAAAAAAAAAAAAAAAAAEEEEEEEEEEEIIIIIOOOOOOOOOOOOOOOOOUUUUUUUUUUUYYYYYD------";
  for (var i=0, l=from.length ; i<l ; i++) {
    str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
  }

  str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
    .replace(/\s+/g, '-') // collapse whitespace and replace by -
    .replace(/-+/g, '-'); // collapse dashes

  return str;
}
function myajaxupload(id, ajaxupload_link, ajaxupload_slug, ajaxupload_path, callback_func, upload_image){
	if(upload_image == 1){
		var uploader = new qq.FileUploader({
			element: document.getElementById(id),
			action: ajaxupload_link,
			allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'],
			sizeLimit: 10485760,   
			minSizeLimit: 0,
			onComplete: function(id, fileName, responseJSON){
				var counter = 0;
				$(".qq-upload-list li").each(function(){
					if(counter == id){
						if(responseJSON.server_filename){
							$(this).find('.qq-upload-file').text(responseJSON.server_filename);
							$(this).hide();
							var filename = ajaxupload_slug + responseJSON.server_filename;
							callback_func(filename);
							$(".qq-upload-list .insert_file").click(function(){
								var filename = ajaxupload_slug + $(this).attr('ref');
								callback_func(filename);
							});
						}
					}
					counter++;
				}); 
			}
		});
	}else{
		var uploader = new qq.FileUploader({
			element: document.getElementById(id),
			action: ajaxupload_link,
			allowedExtensions: ['jpg', 'jpeg', 'png', 'gif', 'zip', 'rar'],
			sizeLimit: 10485760,   
			minSizeLimit: 0,
			onComplete: function(id, fileName, responseJSON){
				var counter = 0;
				$(".qq-upload-list li").each(function(){
					if(counter == id){
						if(responseJSON.server_filename){
							$(this).find('.qq-upload-file').text(responseJSON.server_filename);
							$(this).hide();
							var filename = ajaxupload_slug + responseJSON.server_filename;
							callback_func(filename);
							$(".qq-upload-list .insert_file").click(function(){
								var filename = ajaxupload_slug + $(this).attr('ref');
								callback_func(filename);
							});
						}
					}
					counter++;
				}); 
			}
		});
	}
}
function myfronteditor(editor_id, width, height){
	var editor_language_interface = $("#ckeditor_language_interface").val();
	var editor = CKEDITOR.replace(editor_id,{
		language: editor_language_interface,
		height:height, width:width,
		toolbar :
		[
			{name: 'document', items : [ 'Source']},
			{name: 'basicstyles', items : [ 'Bold','Italic','Underline','Strike']},
			{name: 'paragraph', items : [ 'NumberedList','BulletedList', 'JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock']},
			{name: 'links', items : [ 'Link','Image','Flash','Iframe']},
			{name: 'colors', items : ['FontSize','TextColor','BGColor','Undo','Redo']},
		]
	});
	return editor;
}
$(document).ready(function(){
	$(".slug_title").keyup(function(){
		var val = string_to_slug($(this).val());
		var ref = $(this).attr('ref');
		$("#" + ref).val(val);
	});
	
	try{
		if(!$('#my_form input:visible:enabled:first').hasClass('default_input')){
			$('#my_form input:visible:enabled:first').focus();
		}
	}catch(ex){}
	
	var base_url = $("#base_url_global").val();
	try{
		$( ".datepicker" ).datepicker({
			showOn: "button",
			buttonImage: base_url+"static/assets/images/calendar.gif",
			buttonImageOnly: true,
			dateFormat: "yy-mm-dd"
		});	
	}catch(ex){}
	try{
		$( ".timepicker" ).timepicker({
			showOn: "button",
			buttonImage: base_url+"static/assets/images/calendar.gif",
			buttonImageOnly: true,
			timeFormat: 'hh:mm'
		});
	}catch(ex){}
	
	try{
		$('.scroll-pane').jScrollPane({scrollbarWidth:20, scrollbarMargin:10});
	}catch(ex){}
	
	try{
		$(".colorbox").colorbox({rel:'colorbox',width:"98%", height:"98%"});
	}catch(ex){}
	
	$("#my_form input").keyup(function(event){
		if (event.keyCode == '13') {
			submit_my_form();
		}
	});
	
	$(".submit_button").click(function(){
		$("#my_form").submit();
	});
	
	$(".group_submit_button").click(function(){
		var ref = $(this).attr('ref');
		$("#my_form").attr('action', ref);
		$("#my_form").submit();
	});
	
	$(".submit_close_button").click(function(){
		$("#close_form").val(1);
		$("#my_form").submit();
	});
	$(".checkbox_all_rows").click(function(){
		if($(this).attr('checked')){
			$('.checkbox_row').attr('checked', 'checked');
		}else{
			$('.checkbox_row').removeAttr('checked');
		}
	});
	
	try{
		var fixHelper = function(e, ui) {
		Â Â Â Â ui.children().each(function() {
		Â Â Â Â Â Â Â Â $(this).width($(this).width());
		Â Â Â Â });
		Â Â Â Â return ui;
		};
		var save_link = $("#save_link").val();
		$("#sortable tbody").sortable({
			handle: ".move_handle",
		    helper: fixHelper,
		    stop: function(event, ui) {
	   			var serialized = $("#sortable tbody").sortable('serialize');
				$.ajax({
		            type: "POST",
		            url: save_link,
		            data: serialized,
		            success: function (msg) {
		            	show_info_message(msg);
		            }
		        });
	   		}
		}).disableSelection();
	}catch(ex){}
	$('.hqtip').each(function(){
      $(this).qtip({
         content: $(this).attr('qtip'),
         position: { target: 'mouse' },
         style: { width: 500,padding: 5, fontSize:"13px", lineHeight:"20px", background: '#E5F6FE',color: 'black',textAlign: 'justify',border: {width: 1,radius: 2,color: '#ADD9ED'},name: 'dark'}
      });
    });
	$('.nqtip').each(function(){
	      $(this).qtip({
	         content: $(this).attr('qtip'),
	         position: { target: 'mouse' },
	         style: {width:'100%', padding: 1,background: '#E5F6FE',color: 'black',textAlign: 'justify',border: {width: 1,radius: 2,color: '#ADD9ED'},name: 'dark'}
	      });
	    });
	$('.default_input').focus(function(){
		if ($(this).val() == $(this).attr('default_val')){
           $(this).val('');
        }
	});
	$('.default_input').blur(function(){
		if ($(this).val() == '') {
	        $(this).val($(this).attr('default_val'));
	    }
	});
	$('.my_toggle_content_box').hide();
	$('.my_toggle_title').click(function() {
		var ref_id = $(this).attr('ref_id');
		$('#' + ref_id).toggle('slow', function() {});
	});
});